﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using DingApi.Models;
using System.Data;

using System.Web;
using Dingding.Helper;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

/*
 * 由于调拨通知没有指定：包装方式；
 * 原二维码也仅能管理到批次，没法管理到包装方式；
 * 
 * 2023年11月28日与徐志强确认，老的标签暂时不暂时不考虑；
 * 次调拨仅支持有材料编码的，且材料编码的仅能通过手持挑拨实现【若通过电脑是不行的serve不同，也具有可操作性，电脑端很难道具体材料，只能到批号；】
 
   
     */

namespace DingApi.Controllers
{
    [RoutePrefix("api/Ym02dbo")]
    public class Ym02dboController : ApiController
    {

        /// <summary>
        /// /调拨通知单；要求，调出有明确库区、调入有明确库区、产品有规格明细；
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm02dbo_Bill_inq")]
        public System.Data.DataSet PostYm02dbo_Bill_inq([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");



            EI.EIInfo inBlock = new EI.EIInfo();
          

            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? " " : obj["BILL_OF_LADING_NO"].ToString();


            /*  对应画面；MM0029BC;MM0030BC;
             *  对应表 tmm0029 主表；tmm0030 子表；不是所有记录都有主表；
             *  MM0029BC 该画面的记录主表、子表都有； TZ23300375
             *  MM0030BC 只要主表，没有子表，且子记录为空；【子项号为】，只有一条记录；TZ22C00887，主表没有；
             *  故判断采用子表判断；
           
            */
            //string sql_str0 = "select  nvl(substr(stock_char_begin,0,instr(replace(stock_char_begin,'：',':'), ':',-1,1)-1),stock_char_begin)  STOCK_CHAR_BEGIN ";
            //sql_str0 += ", OLD_STOCK_NO ";
            //sql_str0 += ",nvl(substr(STOCK_CHAR_END,0,instr(replace(STOCK_CHAR_END,'：',':'), ':',-1,1)-1),STOCK_CHAR_END)  STOCK_CHAR_END  ";
            //sql_str0 += ",NEW_STOCK_NO,prod_code   from tmm0030  ";
            //sql_str0 += "         where substr(valid_start_date,1,12)<=to_char(sysdate,'yyyymmddHH24MISS')  ";
            //sql_str0 += "          and substr(valid_end_date,1,12)>=to_char(sysdate,'yyyymmddHH24MISS') and bill_status='20'  ";
            //sql_str0 += "          and  iostock_bill_no =  '" + BILL_OF_LADING_NO  + "' ";

            string sql_str0 = "select nvl(tx.PACK_MODE_NAME, ' ') PACK_MODE_NAME, MAT_NUM PICK_NUM ,MAT_WT PICK_WT, 0   MAT_NUM,  0  MAT_WT  "
                            + " ,nvl(trim(IOSTOCK_SUB_NO),iostock_bill_no)    BILL_OF_LADING_SUB_NO "
                            + ",t.*  from tmm0030  t ,tsiqmw0 tx ";
            sql_str0 += "      where  t.pack_mode_code = tx.pack_mode_code and  substr(t.valid_start_date,1,12)<=to_char(sysdate,'yyyymmddHH24MISS')  ";
            sql_str0 += "      and substr(t.valid_end_date,1,12)>=to_char(sysdate,'yyyymmddHH24MISS') and t.bill_status='20'  ";
            sql_str0 += "      and  t.iostock_bill_no =  '" + BILL_OF_LADING_NO + "' ";

             


            EI.EIInfo inblock = EF.Utility.ExecQuery(sql_str0);

            
            if (inblock.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]无效或不再有效期范围内！");               
                return outBlock;
            }
            for (int i=0;i< inblock.Tables[0].Rows.Count;i++)
            {
                if (inblock.Tables[0].Rows[i]["OLD_STOCK_NO"].ToString().Trim() == "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]应明确调出仓库！");
                    return outBlock;
                }
                if (inblock.Tables[0].Rows[i]["NEW_STOCK_NO"].ToString().Trim() == "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]应明确调入仓库！");
                    return outBlock;
                }

                if (inblock.Tables[0].Rows[i]["PROD_CODE"].ToString().Trim() == "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]品名不可为空！");
                    return outBlock;
                }
                if (inblock.Tables[0].Rows[i]["PROD_CODE"].ToString().Trim() == "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]品名不可为空！");
                    return outBlock;
                }
                if (inblock.Tables[0].Rows[i]["SG_SIGN"].ToString().Trim() == "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]钢种不可为空！");
                    return outBlock;
                }
                if (inblock.Tables[0].Rows[i]["MAT_SPEC_DESC"].ToString().Trim() == "")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]规格不可为空！");
                    return outBlock;
                }
                //目前包装方式为空
                //if (inblock.Tables[0].Rows[i]["PACK_MODE_CODE"].ToString().Trim() == "")
                //{
                //    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]包装方式不可为空！");
                //    return outBlock;
                //}
                 


                //if (inblock.Tables[0].Rows[i]["STOCK_CHAR_BEGIN"].ToString().Trim().Contains(","))
                //{
                //    outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]含有2个或多个调出仓库，请拆分成多个调拨通知单！");
                //    return outBlock;
                //}

            }
           


            //调出库区； codeInfo2
            EI.EIInfo codeInfo2 = new EI.EIInfo();
            string sql2;
            //仅指定调出仓库，没有指定库区；
            if (inblock.Tables[0].Rows[0]["OLD_STOCK_NO"].ToString().Trim() == "")
            {

                //sql2 = " SELECT  STOCK_NO CODE , STOCK_NAME CODE_DESC_1_CONTENT   FROM TSIMM01 WHERE";
                //sql2 += " STOCK_CHAR = '" + inblock.Tables[0].Rows[0]["STOCK_CHAR_BEGIN"].ToString().Trim() + "'";
               
                //codeInfo2 = EF.Utility.ExecQuery(sql2);
            }
            else
            {
                String[] pInfos = inblock.Tables[0].Rows[0]["OLD_STOCK_NO"].ToString().Trim().Replace(":", ",").Split(',');
                codeInfo2.Tables[0].Columns.Add("CODE");
                codeInfo2.Tables[0].Columns.Add("CODE_DESC_1_CONTENT");

                for (int iCyc = 0; iCyc < pInfos.Length / 2; iCyc++)
                {
                    codeInfo2.Tables[0].Rows.Add();
                    codeInfo2.Tables[0].Rows[iCyc]["CODE"] = pInfos[iCyc * 2];
                    codeInfo2.Tables[0].Rows[iCyc]["CODE_DESC_1_CONTENT"] = pInfos[iCyc * 2 + 1];
                }
            }




            //调入库区；
            EI.EIInfo codeInfo3 = new EI.EIInfo();
            ////仅指定调入仓库，没有指定库区；
            if (inblock.Tables[0].Rows[0]["NEW_STOCK_NO"].ToString().Trim() == "")
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]应明确调入仓库！");
                return outBlock;

                //sql2 = " SELECT  STOCK_NO||':'||STOCK_NAME    CODE, STOCK_NAME  CODE_DESC_1_CONTENT FROM TSIMM01 WHERE";
                //sql2 += " STOCK_CHAR = '" + inblock.Tables[0].Rows[0]["STOCK_CHAR_END"].ToString().Trim() + "'";
                
                //codeInfo3 = EF.Utility.ExecQuery(sql2);
            }
            else
            {
                String[] pInfos = inblock.Tables[0].Rows[0]["NEW_STOCK_NO"].ToString().Trim().Replace(":", ",").Split(',');

                codeInfo3.Tables[0].Columns.Add("CODE");
                codeInfo3.Tables[0].Columns.Add("CODE_DESC_1_CONTENT");
              

                for (int iCyc = 0; iCyc < pInfos.Length / 2; iCyc++)
                {
                    codeInfo3.Tables[0].Rows.Add();
                    codeInfo3.Tables[0].Rows[iCyc]["CODE"] = pInfos[iCyc * 2] + ":" + pInfos[iCyc * 2 + 1];
                    codeInfo3.Tables[0].Rows[iCyc]["CODE_DESC_1_CONTENT"] = pInfos[iCyc * 2 + 1];
                }
            }



            codeInfo2.Tables[0].TableName = "OLD_STOCK_NO";
            outBlock.Tables.Add(codeInfo2.Tables[0].Copy());


            codeInfo3.Tables[0].TableName = "NEW_STOCK_NO";
            outBlock.Tables.Add(codeInfo3.Tables[0].Copy());


            inblock.Tables[0].TableName = "TMM0030";
            outBlock.Tables.Add(inblock.Tables[0].Copy());






            //2 已经扫描的材料明细；
            string sql_str1 = " SELECT ta.PONO_NO ,tb.MOVE_STOCK_APPLY_NO FROM TMM0033 ta, tmm0031 tb  "
                + " where ta.IOSTOCK_BILL_NO = tb.MOVE_STOCK_APPLY_NO  "
                + " and ta.company_code = 'LT' and tb.branch_code = 'N'  "
                + " and tb.bill_status = '10' ";

            sql_str1 += "   and   ta.IOSTOCK_APPLY_NO = '" + BILL_OF_LADING_NO + "' ";
            sql_str1 += "   order   by ta.iostock_sub_no desc";

            EI.EIInfo BInfo_B2 = EF.Utility.ExecQuery(sql_str1);



            //已经扫描的材料明细；
            BInfo_B2.Tables[0].TableName = "Table2";
            outBlock.Tables.Add(BInfo_B2.Tables[0].Copy());

















            outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, outBlock.sys_info.msg);            
            return outBlock;


        }


       

        /// <summary>
        /// 调拨出库，扫描材料
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("PostYm02dbo_mat_no_inq")]
        public System.Data.DataSet PostYm02dbo_mat_no_inq([FromBody]  JObject obj)
        {
            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");





            // MAT_NO，PROD_LOT_NO 必须有一个值；钉钉前端校验；
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();
            string STOCK_NO = obj["STOCK_NO"] == null ? "" : obj["STOCK_NO"].ToString();
            string MAT_NO = obj["MAT_NO"] == null ? "" : obj["MAT_NO"].ToString();
            string PROD_LOT_NO = obj["PROD_LOT_NO"] == null ? "" : obj["PROD_LOT_NO"].ToString();


            string BILL_OF_LADING_SUB_NO = "";//允许的提单子项；




            string sql_temp = "";
            EI.EIInfo Block_temp = new EI.EIInfo();
            EI.EIInfo Block_TPMMM02N = new EI.EIInfo();
            EI.EIInfo Block_TSOSD03 = new EI.EIInfo();
            EI.EIInfo Block_TMM0001 = new EI.EIInfo();


           
            //前台保证不可为空；
            if (MAT_NO.Length> 5)
            {
                //1.1 材料自身校验；
                string sql_str0 = " select STATUS,PROD_CODE,PROD_LOT_NO,qual_grade_code ";
                sql_str0 += "       ,pack_mode_code,stock_no,mat_spec_desc";
                sql_str0 += "       ,SG_STD,SG_SIGN      ";
                sql_str0 += "       ,MAT_NUM,MAT_WT      ";

                sql_str0 += "       from TPMMM02N                                                                                  ";
                sql_str0 += "       where mat_no = '" + MAT_NO + "' ";

                Block_TPMMM02N = EF.Utility.ExecQuery(sql_str0);
                if (Block_TPMMM02N.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不存在！");
                    return outBlock;
                }

                //验证该材料是否满足提单的要求；【仓库、包装方式、质量等级、规格、标准；】
                if (Block_TPMMM02N.Tables[0].Rows[0]["STOCK_NO"].ToString() != STOCK_NO)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不在库区[" + STOCK_NO + "]！");
                    return outBlock;
                }


                //验证材料状态
                if (Block_TPMMM02N.Tables[0].Rows[0]["STATUS"].ToString() != "19")
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]的状态不满足挑拨出库要求！");
                    return outBlock;
                }






                //1.2  材料与调拨通知单校验；目前调拨程序无质量等级、包装方式为空，待优化调拨程序；
                sql_temp = "    select nvl(trim(IOSTOCK_SUB_NO),iostock_bill_no)    BILL_OF_LADING_SUB_NO "
                           + "    from tmm0030   "
                           + "     WHERE    IOSTOCK_BILL_NO = '" + BILL_OF_LADING_NO + "'"
                           + "     and    PROD_CODE = '" + Block_TPMMM02N.Tables[0].Rows[0]["PROD_CODE"].ToString() + "' "
                           + "     and      SG_SIGN ='" + Block_TPMMM02N.Tables[0].Rows[0]["SG_SIGN"].ToString() + "' "
                           + "     and     MAT_SPEC_DESC ='" + Block_TPMMM02N.Tables[0].Rows[0]["MAT_SPEC_DESC"].ToString() + "' "
                           + "     and      PACK_MODE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["PACK_MODE_CODE"].ToString() + "' "
                           ;

                
                //if (Block_TPMMM02N.Tables[0].Rows[0]["QUAL_GRADE_CODE"].ToString() != "1")
                //{
                //    sql_temp = sql_temp + " and QUAL_GRADE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["QUAL_GRADE_CODE"].ToString() + "' ";
                //}

                Block_TSOSD03 = EF.Utility.ExecQuery(sql_temp);
                if (Block_TSOSD03.Tables[0].Rows.Count < 1)
                {
                    outBlock.Tables["SYS"].Rows.Add(-1, "材料号[" + MAT_NO + "]不满足调拨单要求，请确认品名、钢种、规格、包装方式！");
                    return outBlock;
                }

            }
            else //批次管理【老标签，查找满足的提单子项】
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "请扫描或输入有效材料[" + MAT_NO + "]！");
                return outBlock;

                //sql_temp = "    SELECT nvl(trim(IOSTOCK_SUB_NO),iostock_bill_no)   BILL_OF_LADING_SUB_NO "
                //         + "    from tmm0030   "
                //         + "     WHERE    IOSTOCK_BILL_NO = '" + BILL_OF_LADING_NO + "'"
                //         + "     AND      (PROD_CODE,SG_SIGN,ORD_SPEC_DESC) in  "
                //         + "     AND     (select PROD_CODE,SG_SIGN,MAT_SPEC_DESC  "
                //         + "    FROM TMM0001   STOCK_NO= '" + STOCK_NO + "'  "
                //         + "    AND LOT_NO ='" + PROD_LOT_NO + "'"
                //         + "    AND MAT_NUM  >1  AND MAT_WT >0 ) ";

                //Block_TSOSD03 = EF.Utility.ExecQuery(sql_temp);
                //if (Block_TSOSD03.Tables[0].Rows.Count < 1)
                //{
                //    outBlock.Tables["SYS"].Rows.Add(-1, "该批次材[" + PROD_LOT_NO + "]的材料不满足提单要求，请确认品名、钢种、规格！");
                //    return outBlock;
                //}

            }

            //2.1 本材料可以满足的提单子项集合；
            for (int i = 0; i < Block_TSOSD03.Tables[0].Rows.Count; i++)
            {
                BILL_OF_LADING_SUB_NO = BILL_OF_LADING_SUB_NO + Block_TSOSD03.Tables[0].Rows[i]["BILL_OF_LADING_SUB_NO"].ToString() + ",";
            }

             

            //3.1 根据扫描材料号，获取出库物料主档信息；
            //根据【标签.批次号 + 提单子项.规格 物料主档表找材料及材料是否存在】
            //物料主档：PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE + STOCK_NO + PROD_LOT_NO 基本唯一了；
            //极少数  质量等级不一致QUAL_GRADE_CODE或标准不一致；【材料处置或改判】
            //发货不到标准，但材料是管理到标准的；
            sql_temp = " SELECT   MAT_NO, '" + MAT_NO + "'  CUST_MAT_NO  "
                        + ",'" + MAT_NO + "' PONO_NO " //用该字段记录调拨履历表中TMM0033
                        + ",'" + BILL_OF_LADING_NO + "' IOSTOCK_APPLY_NO "

                        + " ,MAT_NUM,    MAT_WT ,ROUND(MAT_WT/MAT_NUM,3)  UNIT_WT_C "
                        + ", ' '  BILL_OF_LADING_SUB_NO ,' ' IOSTOCK_APPLY_SUB_NO " //分别存放可匹配的调拨通知单子项集合、最终匹配的子项（没有子项则匹配主项）
                        + " ,ARCHIVE_FLAG,STOCK_NO,LOT_NO,PROD_CODE,SG_SIGN,MAT_SPEC_DESC,PACK_MODE_CODE  "
                        + " ,QUAL_GRADE_CODE ,SG_STD "
                        + ",PROD_CNAME,SUPPLIER_CODE "
                        + " FROM TMM0001            "
                        + " WHERE STOCK_NO= '" + STOCK_NO + "'  "                      
                        + " AND LOT_NO = '" + Block_TPMMM02N.Tables[0].Rows[0]["PROD_LOT_NO"].ToString() + "' "

                        + " AND PROD_CODE = '" + Block_TPMMM02N.Tables[0].Rows[0]["PROD_CODE"].ToString() + "' "
                        + " AND SG_SIGN ='" + Block_TPMMM02N.Tables[0].Rows[0]["SG_SIGN"].ToString() + "' "
                        + " AND MAT_SPEC_DESC ='" + Block_TPMMM02N.Tables[0].Rows[0]["MAT_SPEC_DESC"].ToString().Replace("M","").Replace("m","") + "' "
                        + " AND PACK_MODE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["PACK_MODE_CODE"].ToString() + "' "

                        + " AND QUAL_GRADE_CODE ='" + Block_TPMMM02N.Tables[0].Rows[0]["QUAL_GRADE_CODE"].ToString() + "' "
                        + " AND SG_STD ='" + Block_TPMMM02N.Tables[0].Rows[0]["SG_STD"].ToString() + "' "
                        ;


            

          Block_TMM0001 = EF.Utility.ExecQuery(sql_temp);
            if (Block_TMM0001.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "该库存主档没有满足该挑拨通知单[" + BILL_OF_LADING_NO + "]的库存！");
                return outBlock;
            } 
            else if (Block_TMM0001.Tables[0].Rows.Count > 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "库存主档中找到多个满足该材料[" + MAT_NO + "]的库存记录，请修复库存主档数据！");
                return outBlock;
            }
            else
            {
                Block_TMM0001.Tables[0].Rows[0]["BILL_OF_LADING_SUB_NO"] = BILL_OF_LADING_SUB_NO; //前台根据此匹配显示grid_view

                Block_TMM0001.Tables[0].Rows[0]["MAT_NUM"] = Block_TPMMM02N.Tables[0].Rows[0]["MAT_NUM"];
                Block_TMM0001.Tables[0].Rows[0]["MAT_WT"] = Block_TPMMM02N.Tables[0].Rows[0]["MAT_WT"];//.ToString();
            }



            ////满足的提单系列，暂时不传前台；
            //Block_TSOSD03.Tables[0].TableName = "TSOSD03";
            //outBlock.Tables.Add(Block_TSOSD03.Tables[0].Copy());

            //可以的材料号，MAT_NUM [MAX_NUM 不校验，一直在变动，有销售出库确认时再校验]
            Block_TMM0001.Tables[0].TableName = "TMM0001";
            outBlock.Tables.Add(Block_TMM0001.Tables[0].Copy());

            outBlock.Tables["SYS"].Rows.Add(0, "");

            return outBlock;

        }



      
        //调拨出库保存【新增操作、修改操作】
        [HttpPost]
        [Route("PostYm02dbo_save")]       
        public System.Data.DataSet PostYm02dbo_save([FromBody]  JObject obj)
        {

            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");





            // MAT_NO，PROD_LOT_NO 必须有一个值；钉钉前端校验；
            string BILL_OF_LADING_NO = obj["BILL_OF_LADING_NO"] == null ? "" : obj["BILL_OF_LADING_NO"].ToString();
            string MOVE_STOCK_APPLY_NO = obj["MOVE_STOCK_APPLY_NO"] == null ? " " : obj["MOVE_STOCK_APPLY_NO"].ToString();

            

            string OLD_STOCK_NO = obj["OLD_STOCK_NO"] == null ? "" : obj["OLD_STOCK_NO"].ToString();
            string NEW_STOCK_NO = obj["NEW_STOCK_NO"] == null ? "" : obj["NEW_STOCK_NO"].ToString();

            string VEHICLE_NO = obj["VEHICLE_NO"] == null ? "" : obj["VEHICLE_NO"].ToString();
             
            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
            string PRO_TYPE = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();


            DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString(), new DingApi.ReadAheadDataTableConverter());;





            //outBlock.Tables["SYS"].Rows.Add("-1", "测试结束");
            //return outBlock;
            //精度丢失，按照第一行的精度转换了。
            //DataTable table_rec = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString());

           // DataTable table_rec2 = JsonConvert.DeserializeObject<DataTable>(obj["table_rec"].ToString(), new DingApi.ReadAheadDataTableConverter());





            EI.EIInfo inblock = new EI.EIInfo();

          






            //MOVE_STOCK_APPLY_NO 必须为空,不为空时，则表示修改，据此新增操作；
            string sql_str0 = "select IOSTOCK_BILL_NO,PROD_CODE, '"+ MOVE_STOCK_APPLY_NO + "'   MOVE_STOCK_APPLY_NO  ,'BC' MOVE_STOCK_TYPE,SUBSTR(OLD_STOCK_NO,1,3)    ";
            sql_str0 += "      OLD_STOCK_NO   ,NEW_STOCK_NO ,'10' BILL_STATUS ,  to_char(sysdate,'yyyymmddHH24MISS')  APPLY_TIME  ";
            sql_str0 += "      ,'NM' COMPANY_CODE, 'N'  BRANCH_CODE ,DPTMENT_CODE   ";
            sql_str0 += "      ,'"+ VEHICLE_NO + "'  VEHICLE_NO   ";
            sql_str0 += "      , (select STOCK_CHAR  from tsimm01 where stock_no =SUBSTR(tmm0030.OLD_STOCK_NO, 1, 3) ) STOCK_CHAR_BEGIN , ";
            sql_str0 += " ( select CODE ||':'|| CODE_DESC_1_CONTENT  from tep0002 where CODE_CLASS='MM59' AND CODE IN (select STOCK_CHAR from tsimm01 where stock_no =SUBSTR(tmm0030.NEW_STOCK_NO, 1, 3))) STOCK_CHAR_end,' ' PROPOSER,0 TOTAL_WT,0 TOTAL_NUM  from tmm0030   ";
            sql_str0 += "         where      substr(valid_start_date,1,12)<=to_char(sysdate,'yyyymmddHH24MISS')  ";
            sql_str0 += "           and substr(valid_end_date,1,12)>=to_char(sysdate,'yyyymmddHH24MISS')and bill_status='20'  ";
            sql_str0 += "           and  iostock_bill_no =  '" + BILL_OF_LADING_NO + "' ";

            inblock = EF.Utility.ExecQuery(sql_str0);
            if (inblock.Tables[0].Rows.Count < 1)
            {
                outBlock.Tables["SYS"].Rows.Add(-1, "错误,挑拨通知单[" + BILL_OF_LADING_NO + "]无效或不再有效期范围内！");
                return outBlock;
            }


            inblock.Tables[0].Rows[0]["OLD_STOCK_NO"] = OLD_STOCK_NO;
            inblock.Tables[0].Rows[0]["NEW_STOCK_NO"] = NEW_STOCK_NO;
            inblock.Tables[0].Rows[0]["MOVE_STOCK_APPLY_NO"] = MOVE_STOCK_APPLY_NO;



            if (inblock.Tables[0].Rows[0]["STOCK_CHAR_END"].ToString().Trim() == "")
            {
                string sql_temp = "    select CODE || ':' || CODE_DESC_1_CONTENT  as TT";
                sql_temp += "    from tep0002    ";
                sql_temp += "    where CODE_CLASS = 'MM59'   ";
                sql_temp += "    AND CODE IN ";
                sql_temp += "    (select STOCK_CHAR  from tsimm01 ";
                sql_temp += "    where stock_no = '" + inblock.Tables[0].Rows[0]["NEW_STOCK_NO"].ToString().Substring(0, 3) + "')";

                EI.EIInfo inblock_temp = EF.Utility.ExecQuery(sql_temp);
                if (inblock_temp.Tables[0].Rows.Count == 1)
                {
                    inblock.Tables[0].Rows[0]["STOCK_CHAR_END"] = inblock_temp.Tables[0].Rows[0]["TT"];
                }
            }

            Decimal TOTAL_WT = 0, TOTAL_NUM = 0;
            for (int i=0;i< table_rec.Rows.Count;i++)
            {
                TOTAL_NUM = TOTAL_NUM + Decimal.Parse(table_rec.Rows[i]["MAT_NUM"].ToString());
                TOTAL_WT =  TOTAL_WT  + Decimal.Parse(table_rec.Rows[i]["MAT_WT"].ToString());

            }

            inblock.Tables[0].Rows[0]["TOTAL_WT"] = TOTAL_WT;
            inblock.Tables[0].Rows[0]["TOTAL_NUM"] = TOTAL_NUM;
            inblock.Tables[0].Rows[0]["PROPOSER"] = USERID;
            



            inblock.Tables.Add("TMM0033_ADD");
            inblock.Tables["TMM0033_ADD"].Merge(table_rec);


            //第二次保存，删除所有子项重新提交；
            if (MOVE_STOCK_APPLY_NO.Trim() != "")
            {
                string str_del= " select *  from  TMM0033   where   iostock_bill_no =  '" + MOVE_STOCK_APPLY_NO + "' ";

                EI.EIInfo inblock_del = EF.Utility.ExecQuery(str_del);
                if (inblock_del.Tables[0].Rows.Count > 0)
                {
                    inblock_del.Tables[0].TableName = "TMM0033_DELETE";

                    //新增删除子表
                    inblock.Tables.Add(inblock_del.Tables[0].Copy());



                    // add by wb 2024-09-11 新增，解决tmm0033.REC_CREATE_TIME 为空问题
                    if (!(inblock.Tables[0].Columns.Contains("REC_CREATE_TIME")))
                   {
                        inblock.Tables[0].Columns.Add("REC_CREATE_TIME");
                   }
                    inblock.Tables[0].Rows[0]["REC_CREATE_TIME"] = inblock_del.Tables[0].Rows[0]["REC_CREATE_TIME"];



                }
            }









            inblock.sys_info.company_code = "LT";
            inblock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inblock.sys_info.userid = USERID;
            inblock.sys_info.username = USERNAME;
            inblock.sys_info.formname = FORMNAME;



            // 调用后台services保存信息 //调拨出库；
            EI.EIInfo outBlock2 = EI.EITuxedo.CallService("mm0031_save", inblock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                //出库成功！  outBlock.Tables["SYS"].Rows.Add(outBlock.sys_info.flag, "出库单号[" + outBlock2.DataSet.Tables[0].Rows[0]["MOVE_STOCK_APPLY_NO"].ToString() + "]");
                string move_stock_apply_no = outBlock2.DataSet.Tables[0].Rows[0]["MOVE_STOCK_APPLY_NO"].ToString();

                EI.EIInfo inblock3 = new EI.EIInfo();
                inblock3.Tables[0].Columns.Add("MOVE_STOCK_APPLY_NO");
                inblock3.Tables[0].Columns.Add("BRANCH_CODE");
                inblock3.Tables[0].Rows.Add(move_stock_apply_no,"N");
                // 调用后台services查询单据信息
                EI.EIInfo outblock = EI.EITuxedo.CallService("mm0031_retrieve", inblock3);
                if (outblock.sys_info.flag != 0)
                {
                    outBlock.Tables["SYS"].Rows.Add(outblock.sys_info.flag, outblock.sys_info.msg);
                    return outBlock;
                }
                else
                {
                    
                    outBlock.Tables.Add(outblock.Tables["TMM0031"].Copy());
                    outBlock.Tables.Add(outblock.Tables["TMM0033"].Copy());

                    outBlock.Tables["SYS"].Rows.Add(0, "成功保存，出库单号["+ move_stock_apply_no+"]");
                    return outBlock;
                }
            }
            

        }



        //调拨确认PA31(19->31);确认回退PB31（31->19);DEL
        [HttpPost]
        [Route("PostYm02dbo_pro")]
        public System.Data.DataSet PostYm02dbo_pro([FromBody]  JObject obj)
        {

            //系统信息；
            EI.EIInfo outBlock = new EI.EIInfo();
            outBlock.Tables.Add("SYS");
            outBlock.Tables["SYS"].Columns.Add("FLAG");
            outBlock.Tables["SYS"].Columns.Add("MSG");

             

        
            string move_stock_apply_no = obj["MOVE_STOCK_APPLY_NO"] == null ? "" : obj["MOVE_STOCK_APPLY_NO"].ToString();
            string pro_type = obj["PRO_TYPE"] == null ? " " : obj["PRO_TYPE"].ToString();






            string USERID = obj["USERID"] == null ? " " : obj["USERID"].ToString();
            string USERNAME = obj["USERNAME"] == null ? " " : obj["USERNAME"].ToString();
            string FORMNAME = obj["FORMNAME"] == null ? " " : obj["FORMNAME"].ToString();
         
             


            EI.EIInfo inblock = new EI.EIInfo();


            inblock.Tables[0].Columns.Add("MOVE_STOCK_APPLY_NO");
            inblock.Tables[0].Rows.Add(move_stock_apply_no);



         

            inblock.sys_info.company_code = "LT";
            inblock.sys_info.company_name = "常熟市龙腾特种钢有限公司";

            inblock.sys_info.userid = USERID;
            inblock.sys_info.username = USERNAME;
            inblock.sys_info.formname = FORMNAME;


            //
            inblock.Tables[0].TableName = pro_type;

            string svr = "pmmm02n_pro";

            if (pro_type.ToUpper()=="DEL") //
            {
                svr = "mm0031_del";
            }


            // 调用后台services保存信息 //调拨出库；
            EI.EIInfo outBlock2 = EI.EITuxedo.CallService(svr, inblock);
            if (outBlock2.sys_info.flag != 0)
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }
            else
            {
                outBlock.Tables["SYS"].Rows.Add(outBlock2.sys_info.flag, outBlock2.sys_info.msg);
                return outBlock;
            }


        }

      
        




    }
}